import os
import shutil

def copy_folders(source_folders, target_folder):
    for source_folder in source_folders:
        folder_name = os.path.basename(source_folder)
        target_path = os.path.join(target_folder, folder_name)

        # 检查目标路径是否存在以及是否为空
        if not os.path.exists(target_path) or not os.listdir(target_path):
            try:
                print(f"开始复制文件夹 {source_folder} 到 {target_path}...")
                shutil.copytree(source_folder, target_path)
                print(f"复制文件夹 {source_folder} 到 {target_path} 完成")
                # 这两个单独的复制语句也可以添加类似检查，但请注意它们的目标是同一个目录 './output'
                gui_source = './Examples/gui.rpy'
                screens_source = './Examples/screens.rpy'
                ttf_source = './Examples/SourceHanSansLite.ttf'
                Questions_source = './Examples/QuestionPage.rpy'
                output_target = './output'

                # 检查gui.rpy是否已存在于output文件夹内
                if not os.path.exists(os.path.join(output_target, 'gui.rpy')):
                    shutil.copy2(gui_source, output_target)
                    print(f"复制文件 '{gui_source}' 到 '{output_target}' 完成")

                # 检查screens.rpy是否已存在于output文件夹内
                if not os.path.exists(os.path.join(output_target, 'screens.rpy')):
                    shutil.copy2(screens_source, output_target)
                    print(f"复制文件 '{screens_source}' 到 '{output_target}' 完成")
                if not os.path.exists(os.path.join(output_target, 'SourceHanSansLite.ttf')):
                    shutil.copy2(ttf_source, output_target)
                    print(f"复制文件 '{ttf_source}' 到 '{output_target}' 完成")
                if not os.path.exists(os.path.join(output_target, 'QuestionPage.rpy')):
                    shutil.copy2(Questions_source, output_target)
                    print(f"复制文件 '{Questions_source}' 到 '{output_target}' 完成")
            except FileExistsError:
                print(f"目标目录下已存在同名文件夹'{folder_name}'，未进行复制。")
            except Exception as e:
                print(f"复制文件夹{source_folder}时发生错误：{str(e)}")
        else:
            print(f"目标路径 {target_path} 已存在内容，跳过复制文件夹 {source_folder}。")

if __name__ == '__main__':
    source_folders = ["./Examples/audio", "./Examples/cache", "./Examples/gui", "./Examples/images", "./Examples/saves",
                      "./Examples/tl"]
    target_folder = "./output"
    copy_folders(source_folders, target_folder)


